// pages/menu/menu.js
let soucang = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alist: [],
    yongliao: [],
    cainame: '',
    isSouCang: false,
    index: null,
    clickcount: 0
  },
  //收藏
  clickMe(e) {
    /* console.log(this.data.clickcount); */
    if (this.data.clickcount == 2) {
      this.setData({
        clickcount: 0
      })
    } else {
      this.data.clickcount++
      console.log(this.data.clickcount);
    }
    let isSouCang = !this.data.isSouCang
    this.setData({
      // 下面本来是这样子的:isSouCang=isSouCang,可以简写
      isSouCang
    })
    //提示用户
    wx.showToast({
      title: isSouCang ? '收藏成功' : '取消收藏',
      icon: 'success'
    })
    if (this.data.clickcount == 1) {
      console.log(this.data.alist);
      console.log(this.data.alist.did);
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/collect?caipu_id=' + this.data.alist.did + "&" + "avator=" + this.data.alist.thumb + '&' + 'title=' + this.data.alist.title + '&' + 'brief=' + this.data.alist.steptext,
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'sessiontoken': wx.getStorageSync('token')

        },
        success: (res) => {
          console.log(res);

        }
      })
    } else {
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/deleteinfo',
        method: 'GET',
        header: {
          'content-type': 'application/json', // 默认值
          'sessiontoken': wx.getStorageSync('token')

        },
        success: (res) => {
          console.log(res);
        }
      })
    }
    /*     let { index } =this.data;
        wx.getStorage({
          key: 'isSouCang',
          success:(res)=>{
            let obj = res.data
            obj[index] = isSouCang;
            wx.setStorage({
              data: obj,
              key: 'isSouCang',
              success:(res)=>{
                console.log(res);
              }
            })
          }
        }) */
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.info);
    var that = this
    var info = null;
    if (options.info == undefined) {
      wx.request({
        url: 'https://www.daizhongdai.xyz:3007/classify?name=' + options.name,
        success: res => {
          console.log(res.data.data);
          info = res.data.data.data[0]
          that.parseInfo(info)
        }
      })
      return
    }
    info = JSON.parse(decodeURIComponent(options.info));
    this.parseInfo(info)




    let detailStorage = wx.getStorageSync('isSouCang')
    /* //如果没有收藏
    if (!detailStorage){
      //初始化一个空的对象
      wx.setStorageSync('isSouCang', {});
    } */
    //如果收藏了
    /* if (detailStorage[index]){
      this.setData({
        isSouCang: true
      })
    } */
  },
  parseInfo: function (info) {
    console.log(info);
    if (info.yl != null) {
      var ylArr = info.yl.split(" # ")
      var yl = ""
      ylArr.forEach(ele => {
        yl += ele
      })
      this.setData({
        yongliao: yl
      })

    }
    if (info.fl != null) {
      var flArr = info.fl.split(" # ")
      var fl = ""
      flArr.forEach(ele => {
        fl += ele
      })
      this.setData({
        xiangqing: fl,
      })
    }



    console.log(info.steptext);
    if (info.steptext != null) {
      var fStep = info.steptext.split("#")
      this.setData({
        step: fStep,
      })
    }
    this.setData({
      alist: info,
      isSouCang:info.isC
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})